﻿/* Page Load */
$(function () {
    $("#btnSubmit").click(SubmitButtonClicked);
});

function SubmitButtonClicked(e) {
    var clientName = $("#txtNameField").val();
    var clientEmail = $("#txtEmailField").val();
    var clientMesasge = $("#txtMsgField").val();

    // Make sure a name was provided
    if (clientName == '') {
        ShowDialogThenFocus("#pnlMessageName", "#txtNameField");
    }
    // Make sure an email was provided
    else if (clientEmail == '') {
        ShowDialogThenFocus("#pnlMessageEmail", "#txtEmailField");
    }
    // Make sure a message was provided
    else if (clientMesasge == '') {
        ShowDialogThenFocus("#pnlMessageMsg", "#txtMsgField");
    }
    // Everything checks out, send the email!
    else {
        var data = '{' +
            '"clientName":"' + clientName + '",' +
            '"clientEmail":"' + clientEmail + '",' +
            '"clientMessage":"' + clientMesasge + '"' +
        '}';

        AJAXWrapper('Contact.aspx/SubmitClientMessage', data,
            SubmitButtonClicked_Success, SubmitButtonClicked_Failure);

        function SubmitButtonClicked_Success(result) {
            if (result.d = 'true') {
                ShowDialog("#pnlMessageSuccess");

                $("#txtNameField").val('');
                $("#txtEmailField").val('');
                $("#txtMsgField").val('');
            }
            else {
                ShowDialog("#pnlMessageFailure");
            }
        }

        function SubmitButtonClicked_Failure(error) {
            ShowDialog("#pnlMessageFailure");
        }
    }
}

function ShowDialog(dialogElement) {
    $(dialogElement).dialog({
        modal: true,
        buttons: {
            Ok: function () {
                $(this).dialog("close");
            }
        },
        width: 500
    });
}

function ShowDialogThenFocus(dialogElement, focusElement) {
    $(dialogElement).dialog({
        modal: true,
        buttons: {
            Ok: function () {
                $(this).dialog("close");
                $(focusElement).focus();
            }
        },
        width: 500
    });
}